﻿using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL
{
    public class MiscHelpers
    {
        static public System.Xml.Linq.XElement CreateElement(System.Xml.Linq.XElement i_ParentXElement, string i_ElementName, string i_NamespaceURI, string i_NamespacePrefix, bool i_AlwaysCreateNewElement)
        {
            System.Xml.Linq.XElement XElement = null;

            bool CreateNewElement = i_AlwaysCreateNewElement;
            System.Xml.Linq.XNamespace XNamespace = i_NamespaceURI;

            if (!i_AlwaysCreateNewElement)
            {
                //  See if node already exists
                if (XNamespace != null)
                    XElement = i_ParentXElement.Element(XNamespace + i_ElementName);
                else
                    XElement = i_ParentXElement.Element(i_ElementName);

                CreateNewElement = (XElement == null);
            }

            //  Determine if we should create new node
            if (CreateNewElement)
            {
                //  Create and append new node
                if (XNamespace != null)
                    XElement = new System.Xml.Linq.XElement(XNamespace + i_ElementName);
                else
                    XElement = new System.Xml.Linq.XElement(i_ElementName);

                i_ParentXElement.Add(XElement);

                if (!System.String.IsNullOrEmpty(i_NamespacePrefix))
                {
                    System.Xml.Linq.XAttribute XAttribute = new System.Xml.Linq.XAttribute(System.Xml.Linq.XNamespace.Xmlns + i_NamespacePrefix, XNamespace);
                    XElement.Add(XAttribute);
                }
            }

            return XElement;
        }

        static public System.Xml.Linq.XElement CreateElementHierarchy(System.Xml.Linq.XElement i_ParentXElement, string[] i_ElementNames, string[] i_NamespaceURIs, string[] i_NamespacePrefixes)
        {
            return Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.MiscHelpers.CreateElementHierarchy
                (
                i_ParentXElement,
                i_ElementNames,
                i_NamespaceURIs,
                i_NamespacePrefixes,
                false
                );
        }

        static public System.Xml.Linq.XElement CreateElementHierarchy(System.Xml.Linq.XElement i_ParentXElement, string[] i_ElementNames, string[] i_NamespaceURIs, string[] i_NamespacePrefixes, bool i_AlwaysCreateLastElement)
        {
            System.Xml.Linq.XElement XElement = null;
            System.Xml.Linq.XElement ParentXElement = i_ParentXElement;

            for (int Index = 0; Index < i_ElementNames.Length; ++Index)
            {
                string ElementName = i_ElementNames[Index];
                string NamespaceURI = i_NamespaceURIs[Index];
                string NamespacePrefix = i_NamespacePrefixes[Index];

                bool ForceElementCreation = (i_AlwaysCreateLastElement && (Index == i_ElementNames.Length - 1));

                XElement = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.MiscHelpers.CreateElement
                    (
                    ParentXElement,
                    ElementName,
                    NamespaceURI,
                    NamespacePrefix,
                    ForceElementCreation
                    );

                ParentXElement = XElement;
            }

            return XElement;
        }

        static public string[] GetFieldIDsForExpression(string i_ExpressionString)
        {
            System.Collections.Generic.List<string> FieldIds = new System.Collections.Generic.List<string>();

            string[] LeftExpressionParts = i_ExpressionString.Split(new char[] { '{' });
            for (int Index = 0; Index < LeftExpressionParts.Length; ++Index)
            {
                int RightParenthesisIndex = LeftExpressionParts[Index].LastIndexOf("}");
                if (RightParenthesisIndex == -1)
                    continue;

                string FieldID = LeftExpressionParts[Index].Substring(0, RightParenthesisIndex);
                FieldIds.Add(FieldID);
            }

            return FieldIds.ToArray();
        }

    }
}
